package com.bianxingji.api;


import com.bianxingji.model.PageParams;
import com.bianxingji.model.PageResult;
import com.bianxingji.model.RestResponse;
import com.bianxingji.model.dto.QueryMediaParamsDto;
import com.bianxingji.po.BxjMediaFiles;
import com.bianxingji.service.BxjMediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
 @RequestMapping("/media")
public class MediaFilesController {


  @Autowired
  BxjMediaFileService bxjMediaFileService;


 @ApiOperation("媒资列表查询接口")
 @PostMapping("/files")
 public PageResult<BxjMediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
  Long companyId = 1232141425L;
  return bxjMediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);
 }

    @ApiOperation("根据媒资Id获取媒资路径")
    @GetMapping("/{id}")
    public String getMediaPath(@PathVariable String id) {
        return bxjMediaFileService.getUrlById(id);
    }

    @ApiOperation(value = "文件上传前检查文件")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkfile(
            @RequestParam("fileMd5") String fileMd5
    ) throws Exception {
        RestResponse<Boolean> booleanRestResponse = bxjMediaFileService.checkFile(fileMd5);
        return booleanRestResponse;
    }

    @ApiOperation(value = "分块文件上传前的检测")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                            @RequestParam("chunk") int chunk) throws Exception {
        RestResponse<Boolean> booleanRestResponse = bxjMediaFileService.checkChunk(fileMd5,chunk);
        return booleanRestResponse;
    }

    @ApiOperation(value = "上传分块文件")
    @PostMapping(value = "/upload/uploadchunk",consumes = "multipart/form-data")
    public RestResponse uploadchunk(@RequestParam("file") MultipartFile file,
                                    @RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("chunk") int chunk) throws Exception {

        //创建一个临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        file.transferTo(tempFile);
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();

        RestResponse restResponse = bxjMediaFileService.uploadChunk(fileMd5, chunk, localFilePath);

        return restResponse;
    }


    @ApiOperation(value = "上传图片")
    @PostMapping(value = "/upload/uploadImg",consumes = "multipart/form-data")
    public RestResponse uploadImg(@RequestParam("file") MultipartFile file) throws Exception {
        RestResponse restResponse = bxjMediaFileService.uploadImg(file);

        return restResponse;
    }

    @ApiOperation(value = "合并文件")
    @PostMapping("/upload/mergechunks")
    public RestResponse mergechunks(@RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("fileName") String fileName,
                                    @RequestParam("chunkTotal") int chunkTotal) throws Exception {
        //文件信息对象
        BxjMediaFiles bxjMediaFiles=new BxjMediaFiles();
        bxjMediaFiles.setFilename(fileName);
        bxjMediaFiles.setTags("视频文件");
        bxjMediaFiles.setFileType("001002");
        RestResponse restResponse = bxjMediaFileService.mergechunks(fileMd5, chunkTotal, bxjMediaFiles);
        return restResponse;
    }

}
